package com.hnyyjsxy.web.servlet;

import com.hnyyjsxy.domain.*;
import com.hnyyjsxy.service.TeacherService;
import com.hnyyjsxy.service.impl.StudentServiceImpl;
import com.hnyyjsxy.service.impl.TeacherServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @date 2021/02/05
 */
@WebServlet("/teacher_html/tea/*")
public class TeacherServlet extends BeanServlet {
    TeacherService teacherService = new TeacherServiceImpl();

    /**
     * 获取老师的具体信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void getTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 从session中获取登录成功的老师账号信息
        AccountSheet user = (AccountSheet) request.getSession().getAttribute("user");
        //2. 通过账号查询该宿管的详细信息
        Teacher teacher = teacherService.getTeacherByAccount(user.getAccount());
        //3. 响应
        respondOnly(teacher,response);
    }

    /**
     * 宿管退出登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 销毁session,这个方法就是session自己将自己弄死
        //使此会话无效，然后取消对任何绑定到它的对象的绑定。
        request.getSession().invalidate();
        //2. 重定向至登录界面
        response.sendRedirect(request.getContextPath()+"/index.html");
    }

    /**
     * 学生申请调换宿舍 先查询该床位是否以有人
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void queryBed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String building = request.getParameter("building");
        String room = request.getParameter("room");
        String bed = request.getParameter("bed");
        //2. 调用业务层，返回值为boolean类型 如果为true就表示该床位以有人
        boolean flag = teacherService.queryBedExist(building, room, bed);
        //3. 响应
        respondOnly(flag,response);
    }

    /**
     * 查询该楼栋下的所有学生调换宿舍的申请
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void queryApply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 从session中获取登录成功的老师账号信息
        AccountSheet user = (AccountSheet) request.getSession().getAttribute("user");
        //2. 通过账号查询该宿管的详细信息
        Teacher teacher = teacherService.getTeacherByAccount(user.getAccount());
        //3. 根据该宿管老师管理的楼栋号查询
        List<ApplyFor> list = teacherService.queryStudentApply(teacher.getBuilding());
        //4. 响应
        respondOnly(list,response);
    }

    /**
     * 处理学生调换宿舍的申请
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void disposeApply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数：学号+房间号+床号+处理结果
        String sid = request.getParameter("sid");
        String room = request.getParameter("room");
        String bed = request.getParameter("bed");
        String flag = request.getParameter("flag");
        //还需要通过学号获取学生的邮箱，以便将处理结果告知他
        String email = new StudentServiceImpl().getStudentBySid(sid).getEmail();
        //2. 将请求产生传给业务层
        teacherService.disposeStudentApply(sid,room,bed,flag,email);
        //3. 重定向至宿管查询调换宿舍申请的html页面
        response.sendRedirect(request.getContextPath()+"/teacher_html/query_apply_for.html");
    }

    /**
     * 添加违纪情况
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addViolate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String sid = request.getParameter("sid");
        String cause = request.getParameter("cause");
        //解决get请求中文乱码问题
        cause = new String(cause.getBytes("iso-8859-1"),"utf-8");
        //2. 判断该学号是否存在
        Student student = new StudentServiceImpl().getStudentBySid(sid);
        if (student != null){
            //3. 将请求参数传给业务层
            teacherService.addStudentViolate(sid,cause);
            respondOnly(true,response);
        }else{
            //该学号不存在
            respondOnly(false,response);
        }
    }

    /**
     * 添加外访人员记录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addStranger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Stranger stranger = new Stranger();
        try {
            BeanUtils.populate(stranger,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //2. 调用业务层
        teacherService.addStrangerRecord(stranger);
    }

    /**
     * 查询外访人员记录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void queryStranger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String sname = request.getParameter("sname");
        String page = request.getParameter("page");
        //2. 将当前页类型转换为int
        int curpage = 1;
        if (page!= null && page.length()>0 && !"null".equals(page)){
            curpage = Integer.parseInt(page);
        }
        //3. 查询登录成功后宿管管理的楼栋号
        AccountSheet user = (AccountSheet) request.getSession().getAttribute("user");
        Teacher teacher = teacherService.getTeacherByAccount(user.getAccount());
        //4. 将请求参数传给业务层，返回的是一个PageBean对象
        PageBean pageBean = teacherService.queryStrangerRecord(sname, curpage, teacher.getBuilding());
        //5. 响应
        respondOnly(pageBean,response);
    }

    /**
     * 门禁系统
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void doorControl(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String idCard = request.getParameter("id_card");
        //2. 调用业务层，查询该卡号是否正确，返回值是一个DoorForbidding对象
        DoorForbidding doorForbidding = teacherService.queryCard(idCard);
        //3. 响应
        respondOnly(doorForbidding,response);
    }

    /**
     * 按照学号删除学生的信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delBySid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String sid = request.getParameter("sid");
        //2. 调用业务层，参数为学号
        teacherService.delStudentBySid(sid);
    }

    /**
     * 通过班级删除学生信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delBySquad(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数中的班级信息
        String squad = request.getParameter("squad");
        //2. 从session中获取登录后的宿管信息，查询它管理的楼栋
        AccountSheet accountSheet = (AccountSheet) request.getSession().getAttribute("user");
        Teacher teacher = teacherService.getTeacherByAccount(accountSheet.getAccount());
        //3. 将班级和楼栋号传给业务层查询学生信息，返回值是一个list集合
        List<Student> list = teacherService.queryStudentBySquad(squad, teacher.getBuilding());
        //4. 遍历这个list集合，取出学号，通过学号删除学生信息
        for (Student student : list) {
            teacherService.delStudentBySid(student.getSid());
        }
    }
}
